/* eslint no-unused-vars:0 */
import React from 'react';
import './index.less';
import {
    Menu,
    Col,
    Row,
    Icon,
    Input,
    Select,
    Button,
    Table,
    LocaleProvider,
    DatePicker,
    message,
    Form,
    Modal,
    Tooltip,
    Cascader,
    Checkbox,
    AutoComplete
} from 'antd';
// 由于 antd 组件的默认文案是英文，所以需要修改为中文
import zhCN from 'antd/lib/locale-provider/zh_CN';
import moment from 'moment';
import 'moment/locale/zh-cn';
import 'antd/dist/antd.css';

const Option = Select.Option;
const ButtonStyle = {
    marginLeft: '0px',
    marginRight: '40px'
};
const deleteButtonStyle = {
    marginLeft: '-10px',
    marginRight: '40px'
};

moment.locale('zh-cn');

export default class RiskAssessmentManageParamManage extends React.Component {
    state = {
        data: [],
        riskTypes: [],
        riskTypeDelete: '请选择',
        riskTypeDeleteSuccess: '选择为空',
        DeleteTypeVis: false,
        typeInputId: 'typeInputId',
        typeInput: '',

        AddTipVis: false,

        riskLevels: [],
        riskLevelDelete: '',
        riskLevelDeleteSuccess: '选择为空',
        DeleteLevelVis: false,
        levelInputId: 'levelInputId',
        levelInput: '',

        riskStatus: [],
        riskStatusDelete: '',
        riskStatusDeleteSuccess: '选择为空',
        DeleteStatusVis: false,
        statusInputId: 'statusInputId',
        statusInput: '',

        referenceLevels: [],
        referenceLevelDelete: '',
        referenceLevelDeleteSuccess: '选择为空',
        DeleteReferenceLevelVis: false,
        referenceLevelInputId: 'statusInputId',
        referenceLevelInput: '',

        advisory: ''
    };

    findRiskTypes() {
        this.$get('/risk/findAllRiskTypes')
            .then((res) => {
                this.setState({
                    riskTypes: res.riskTypes,
                    riskTypeDelete: ''
                });
            })
            .catch((e) => {
                console.log(e);
            });
    }

    findLevelTypes() {
        this.$get('/risk/findAllRiskLevels')
            .then((res) => {
                this.setState({
                    riskLevels: res.riskLevels,
                    riskLevelDelete: ''
                });
            })
            .catch((e) => {
                console.log(e);
            });
    }

    findStatusTypes() {
        this.$get('/riskSolution/findAllDealStatus')
            .then((res) => {
                this.setState({
                    riskStatus: res.allDealStatus,
                    riskStatusDelete: ''
                });
            })
            .catch((e) => {
                console.log(e);
            });
    }

    findReferenceLevels() {
        this.$get('/riskSolution/findAllReferenceLevels')
            .then((res) => {
                this.setState({
                    referenceLevels: res.allReferenceLevels,
                    referenceLevelDelete: ''
                });
            })
            .catch((e) => {
                console.log(e);
            });
    }

    findAdvisory() {
        this.$get('/riskSolution/findAdvisory')
            .then((res) => {
                this.setState({
                    advisory: res.advisory[0].phone
                });
            })
            .catch((e) => {
                console.log(e);
            });
    }

    handleRiskTypeChange(param, value) {
        if (value) {
            this.state.riskTypeDelete = value.label;
            this.forceUpdate();
        } else {
            this.state.riskTypeDelete = '';
            this.forceUpdate();
        }
    }

    handleRiskLevelChange(param, value) {
        if (value) {
            this.state.riskLevelDelete = value.label;
            this.forceUpdate();
        } else {
            this.state.riskLevelDelete = '';
            this.forceUpdate();
        }
    }

    handleRiskStatusChange(param, value) {
        if (value) {
            this.state.riskStatusDelete = value.label;
            this.forceUpdate();
        } else {
            this.state.riskStatusDelete = '';
            this.forceUpdate();
        }
    }

    handleReferenceLevelChange(param, value) {
        if (value) {
            this.state.referenceLevelDelete = value.label;
            this.forceUpdate();
        } else {
            this.state.referenceLevelDelete = '';
            this.forceUpdate();
        }
    }

    handleDeleteType(data) {
        if ('请选择' == this.state.riskTypeDelete) {
            this.setState({
                DeleteTypeVis: true
            });
            return;
        }

        let deleteType = {
            type: this.state.riskTypeDelete
        };
        this.$get('/risk/deleteRiskType', deleteType)
            .then((res) => {
                if (res.msg == 'success') {
                    this.state.riskTypeDeleteSuccess = '删除成功,请刷新页面';
                } else {
                    if (
                        '' == this.state.riskTypeDelete ||
                        '请选择' === this.state.riskTypeDelete
                    ) {
                        this.state.riskTypeDeleteSuccess =
                            '选择为空或已经删除,请刷新页面';
                    } else {
                        this.state.riskTypeDeleteSuccess =
                            '删除失败,已经有风险事件关联';
                    }
                }

                this.setState({
                    DeleteTypeVis: true,
                    riskTypeDelete: ''
                });
            })
            .catch((e) => {
                this.setState({
                    DeleteTypeVis: false,
                    riskTypeDelete: ''
                });
                console.log(e);
            });
    }

    handleDeleteLevel(data) {
        if ('请选择' == this.state.riskLevelDelete) {
            this.setState({
                DeleteLevelVis: true
            });
            return;
        }

        let deleteLevel = {
            level: this.state.riskLevelDelete
        };
        this.$get('/risk/deleteRiskLevel', deleteLevel)
            .then((res) => {
                if (res.msg == 'success') {
                    this.state.riskLevelDeleteSuccess = '删除成功,请刷新页面';
                } else {
                    if (
                        '' == this.state.riskTypeDelete ||
                        '请选择' === this.state.riskTypeDelete
                    ) {
                        this.state.riskLevelDeleteSuccess =
                            '选择为空或已经删除,请刷新页面';
                    } else {
                        this.state.riskLevelDeleteSuccess =
                            '删除失败,已经有风险事件关联';
                    }
                }

                this.setState({
                    DeleteLevelVis: true,
                    riskLevelDelete: ''
                });
            })
            .catch((e) => {
                this.setState({
                    DeleteLevelVis: false,
                    riskLevelDelete: ''
                });
                console.log(e);
            });
    }

    handleDeleteStatus(data) {
        if ('请选择' == this.state.riskStatusDelete) {
            this.setState({
                DeleteStatusVis: true
            });
            return;
        }
        let deleteStatus = {
            status: this.state.riskStatusDelete
        };
        this.$get('/riskSolution/deleteDealStatus', deleteStatus)
            .then((res) => {
                if (res.msg == 'success') {
                    this.state.riskStatusDeleteSuccess = '删除成功,请刷新页面';
                } else {
                    if (
                        '' == this.state.riskTypeDelete ||
                        '请选择' === this.state.riskTypeDelete
                    ) {
                        this.state.riskStatusDeleteSuccess =
                            '选择为空或已经删除,请刷新页面';
                    } else {
                        this.state.riskStatusDeleteSuccess =
                            '删除失败,已经有风险事件关联';
                    }
                }

                this.setState({
                    DeleteStatusVis: true,
                    riskStatusDelete: ''
                });
            })
            .catch((e) => {
                this.setState({
                    DeleteStatusVis: true,
                    riskStatusDelete: ''
                });
                console.log(e);
            });
    }

    handleDeleteReferenceLevel(data) {
        if ('请选择' == this.state.referenceLevelDelete) {
            this.setState({
                DeleteStatusVis: true
            });
            return;
        }
        let deleteReference = {
            level: this.state.referenceLevelDelete
        };
        this.$get('/riskSolution/deleteReferenceLevel', deleteReference)
            .then((res) => {
                if (res.msg == 'success') {
                    this.state.referenceLevelDeleteSuccess =
                        '删除成功,请刷新页面';
                } else {
                    if (
                        '' == this.state.referenceLevelDelete ||
                        '请选择' === this.state.referenceLevelDelete
                    ) {
                        this.state.referenceLevelDeleteSuccess =
                            '选择为空或已经删除,请刷新页面';
                    } else {
                        this.state.referenceLevelDeleteSuccess =
                            '删除失败,已经有风险事件关联';
                    }
                }

                this.setState({
                    DeleteReferenceLevelVis: true,
                    referenceLevelDelete: ''
                });
            })
            .catch((e) => {
                this.setState({
                    DeleteReferenceLevelVis: true,
                    referenceLevelDelete: ''
                });
                console.log(e);
            });
    }

    handleAddTypeChange(e) {
        this.setState({
            typeInput: e.target.value
        });
    }

    handleAddLevelChange(e) {
        this.setState({
            levelInput: e.target.value
        });
    }

    handleAddStatusChange(e) {
        this.setState({
            statusInput: e.target.value
        });
    }

    handleAddReferenceLevelChange(e) {
        this.setState({
            referenceLevelInput: e.target.value
        });
    }

    handleAddAdvisoryChange(e) {
        this.setState({
            advisory: e.target.value
        });
    }

    handleAddType(data) {
        let addType = {
            type: this.state.typeInput
        };
        this.$get('/risk/addRiskType', addType)
            .then((res) => {
                this.setState({
                    typeInput: '',
                    AddTipVis: true
                });
            })
            .catch((e) => {
                console.log(e);
            });
        console.log(this.state.typeInput);
    }

    handleAddLevel(data) {
        let levelType = {
            level: this.state.levelInput
        };
        this.$get('/risk/addRiskLevel', levelType)
            .then((res) => {
                this.setState({
                    levelInput: '',
                    AddTipVis: true
                });
            })
            .catch((e) => {
                console.log(e);
            });
        console.log(this.state.levelInput);
    }

    handleAddStatus(data) {
        let statusType = {
            status: this.state.statusInput
        };
        this.$get('/riskSolution/addDealStatus', statusType)
            .then((res) => {
                this.setState({
                    statusInput: '',
                    AddTipVis: true
                });
            })
            .catch((e) => {
                console.log(e);
            });
        console.log(this.state.statusInput);
    }

    handleAddReferenceLevel(data) {
        let referenceLevel = {
            level: this.state.referenceLevelInput
        };

        this.$get('/riskSolution/addReferenceLevel', referenceLevel)
            .then((res) => {
                this.setState({
                    referenceLevelInput: '',
                    AddTipVis: true
                });
            })
            .catch((e) => {
                console.log(e);
            });
        console.log(this.state.referenceLevelInput);
    }

    handleAddAdvisory(data) {
        let phoneAdvisory = {
            phone: this.state.advisory
        };
        this.$get('/riskSolution/addAdvisory', phoneAdvisory)
            .then((res) => {
                this.findAdvisory();
                this.setState({
                    AddTipVis: true
                });
            })
            .catch((e) => {
                console.log(e);
            });
        console.log(this.state.advisory);
    }

    locationRefresh(type) {
        this.setState({
            DeleteTypeVis: false,

            AddTipVis: false,
            riskTypeDelete: '',

            DeleteLevelVis: false,
            riskLevelDelete: '',

            DeleteStatusVis: false,
            riskStatusDelete: '',

            DeleteReferenceLevelVis: false,
            referenceLevelDelete: ''
        });
        this.findRiskTypes();
        this.findLevelTypes();
        this.findStatusTypes();
        this.findReferenceLevels();
        this.render();
    }

    locationRefresh2(type) {
        this.setState({
            DeleteTypeVis: false,
            AddTipVis: false
        });
    }

    componentWillMount() {
        this.findRiskTypes();
        this.findLevelTypes();
        this.findStatusTypes();
        this.findReferenceLevels();
        this.findAdvisory();
    }

    render() {
        return (
            <div>
                <Row className="row1">
                    <Col span="22">
                        <span
                            style={{
                                color: '#d6010a',
                                marginLeft: '20px',
                                fontWeight: 'bold'
                            }}>
                            Tip:如需更新则删除对应的再添加新的
                        </span>

                        <Row className="sub-row2">
                            <span style={{ marginRight: '20px' }}>
                                风险事件种类:
                            </span>
                            <Select
                                className="sub-row2-select"
                                placeholder="请选择"
                                labelInValue
                                style={{ width: 200, height: 30 }}
                                onChange={this.handleRiskTypeChange.bind(
                                    this,
                                    'riskTypeDelete'
                                )}>
                                {this.state.riskTypes.map((item) => {
                                    return (
                                        <Option key={item.id} value={item.type}>
                                            {item.type}
                                        </Option>
                                    );
                                })}
                            </Select>
                            <Button
                                type="danger"
                                style={deleteButtonStyle}
                                onClick={this.handleDeleteType.bind(this)}>
                                删除
                            </Button>

                            {
                                <Input
                                    type="text"
                                    placeholder="请输入风险事件种类"
                                    style={{ width: 200, height: 30 }}
                                    id={this.state.typeInputId}
                                    onChange={this.handleAddTypeChange.bind(
                                        this
                                    )}
                                />
                            }
                            <Button
                                type="primary"
                                style={ButtonStyle}
                                onClick={this.handleAddType.bind(this)}>
                                添加
                            </Button>
                        </Row>

                        {/* // ======================  //*/}

                        <Row className="sub-row2">
                            <span style={{ marginRight: '20px' }}>
                                风险预警级别:
                            </span>

                            <Select
                                className="sub-row2-select"
                                placeholder="请选择"
                                labelInValue
                                style={{ width: 200, height: 30 }}
                                onChange={this.handleRiskLevelChange.bind(
                                    this,
                                    'riskLevelDelete'
                                )}>
                                {this.state.riskLevels.map((item) => {
                                    return (
                                        <Option
                                            key={item.id}
                                            value={item.level}>
                                            {item.level}
                                        </Option>
                                    );
                                })}
                            </Select>
                            <Button
                                type="danger"
                                style={deleteButtonStyle}
                                onClick={this.handleDeleteLevel.bind(this)}>
                                删除
                            </Button>

                            {
                                <Input
                                    type="text"
                                    placeholder="请输入风险预警级别"
                                    style={{ width: 200, height: 30 }}
                                    id={this.state.levelInputId}
                                    onChange={this.handleAddLevelChange.bind(
                                        this
                                    )}
                                />
                            }
                            <Button
                                type="primary"
                                style={ButtonStyle}
                                onClick={this.handleAddLevel.bind(this)}>
                                添加
                            </Button>
                        </Row>

                        {/* // ======================  //*/}

                        <Row className="sub-row2">
                            <span style={{ marginRight: '20px' }}>
                                风险处理状态:
                            </span>

                            <Select
                                className="sub-row2-select"
                                placeholder="请选择"
                                labelInValue
                                style={{ width: 200, height: 30 }}
                                onChange={this.handleRiskStatusChange.bind(
                                    this,
                                    'riskStatusDelete'
                                )}>
                                {this.state.riskStatus.map((item) => {
                                    return (
                                        <Option
                                            key={item.id}
                                            value={item.status}>
                                            {item.status}
                                        </Option>
                                    );
                                })}
                            </Select>
                            <Button
                                type="danger"
                                style={deleteButtonStyle}
                                onClick={this.handleDeleteStatus.bind(this)}>
                                删除
                            </Button>

                            {
                                <Input
                                    type="text"
                                    placeholder="请输入风险处理状态"
                                    style={{ width: 200, height: 30 }}
                                    id={this.state.statusInputId}
                                    onChange={this.handleAddStatusChange.bind(
                                        this
                                    )}
                                />
                            }
                            <Button
                                type="primary"
                                style={ButtonStyle}
                                onClick={this.handleAddStatus.bind(this)}>
                                添加
                            </Button>
                        </Row>

                        {/* // ======================  //*/}

                        <Row className="sub-row2">
                            <span style={{ marginRight: '20px' }}>
                                解决方案参考:
                            </span>

                            <Select
                                className="sub-row2-select"
                                placeholder="请选择"
                                labelInValue
                                style={{ width: 200, height: 30 }}
                                onChange={this.handleReferenceLevelChange.bind(
                                    this,
                                    'referenceLevelDelete'
                                )}>
                                {this.state.referenceLevels.map((item) => {
                                    return (
                                        <Option
                                            key={item.id}
                                            value={item.level}>
                                            {item.level}
                                        </Option>
                                    );
                                })}
                            </Select>
                            <Button
                                type="danger"
                                style={deleteButtonStyle}
                                onClick={this.handleDeleteReferenceLevel.bind(
                                    this
                                )}>
                                删除
                            </Button>

                            {
                                <Input
                                    type="text"
                                    placeholder="请输入解决方案参考"
                                    style={{ width: 200, height: 30 }}
                                    id={this.state.referenceLevelInputId}
                                    onChange={this.handleAddReferenceLevelChange.bind(
                                        this
                                    )}
                                />
                            }
                            <Button
                                type="primary"
                                style={ButtonStyle}
                                onClick={this.handleAddReferenceLevel.bind(
                                    this
                                )}>
                                添加
                            </Button>
                        </Row>
                        <Row className="sub-row2">
                            <br /> <br />
                            专家联系方式(联系方式唯一,新的会覆盖旧的)
                        </Row>
                        <Row className="sub-row2">
                            <textarea
                                rows="4"
                                cols="113"
                                value={this.state.advisory}
                                onChange={this.handleAddAdvisoryChange.bind(
                                    this
                                )}
                            />
                        </Row>
                        <div style={{ float: 'right', marginRight: '280px' }}>
                            <Button
                                type="primary"
                                onClick={this.handleAddAdvisory.bind(this)}>
                                确认添加
                            </Button>
                        </div>
                    </Col>
                    <Modal
                        title="提示"
                        visible={this.state.DeleteTypeVis}
                        onCancel={this.locationRefresh.bind(
                            this,
                            'DeleteTypeVis'
                        )}
                        onOk={this.locationRefresh.bind(this, 'DeleteTypeVis')}
                        okText="确定"
                        cancelText="取消">
                        <p>{this.state.riskTypeDeleteSuccess}</p>
                    </Modal>
                    <Modal
                        title="提示"
                        visible={this.state.DeleteLevelVis}
                        onCancel={this.locationRefresh.bind(
                            this,
                            'DeleteLevelVis'
                        )}
                        onOk={this.locationRefresh.bind(this, 'DeleteLevelVis')}
                        okText="确定"
                        cancelText="取消">
                        <p>{this.state.riskLevelDeleteSuccess}</p>
                    </Modal>
                    <Modal
                        title="提示"
                        visible={this.state.DeleteStatusVis}
                        onCancel={this.locationRefresh.bind(
                            this,
                            'DeleteStatusVis'
                        )}
                        onOk={this.locationRefresh.bind(
                            this,
                            'DeleteStatusVis'
                        )}
                        okText="确定"
                        cancelText="取消">
                        <p>{this.state.riskStatusDeleteSuccess}</p>
                    </Modal>
                    <Modal
                        title="提示"
                        visible={this.state.DeleteReferenceLevelVis}
                        onCancel={this.locationRefresh.bind(
                            this,
                            'DeleteReferenceLevelVis'
                        )}
                        onOk={this.locationRefresh.bind(
                            this,
                            'DeleteReferenceLevelVis'
                        )}
                        okText="确定"
                        cancelText="取消">
                        <p>{this.state.referenceLevelDeleteSuccess}</p>
                    </Modal>
                    <Modal
                        title="提示"
                        visible={this.state.AddTipVis}
                        onCancel={this.locationRefresh2.bind(this, 'AddTipVis')}
                        onOk={this.locationRefresh2.bind(this, 'AddTipVis')}
                        okText="确定"
                        cancelText="取消">
                        <p>添加成功,请刷新页面</p>
                    </Modal>
                </Row>
            </div>
        );
    }
}
